
$(function () {
    var user = {
        swiperList: $('.swiper-wrapper'),
        init: function () {
            this.event();
        },
        event: function () {
            // 获取每日推荐
            this.getRecommend();
        },
        // 头部轮播
        swiper: function () {
            var swiper = new Swiper('.swiper-container', {
                pagination: '.swiper-pagination',
                paginationClickable: true,
                speed: 500,
                autoplay: 4000
            });
        },
        // 获取每日推荐
        getRecommend: function() {
            var self = this, ajax = new AjaxHelper();
            ajax.get('/recommend','', function (data) {
                // 判断是否有值
                console.log(data);
                console.log(data.results.length)
                if (data.results.length <= 0) {
                    return false;                     
                } else {
                    self.swiperList.empty();
                    $.each(data.results, function (i, item) {
                        var t = simpleTpl();
                        if (item.elite == true) {
                            var jh = '<i class="iconfont card-type card-type-jh">&#xe633;</i>';
                        } else {
                            var jh = '';
                        }
                        t._('<div class="swiper-slide">')
                            ._('<div class="list-item img-txt-list-item" data-path="/article/detail/'+item._id+'">')
                                ._('<a href="/article/'+item._id+'">')
                                    ._('<div class="pull-right txt-item">')
                                        ._('<p class="item-title">'+item.title+'<span class="type type-'+item.category.slug+'">'+item.category.name+'</span></p>')
                                        ._('<p class="item-desc">'+item.body+'</p>')
                                        ._('<div class="item-other">')
                                            ._('<span class="author"><i class="iconfont">&#xe910;</i><b>'+item.author.username+'</b></span>')
                                            ._('<span class="address"><i class="iconfont">&#xe939;</i>'+item.area+'</span>')
                                            ._('<span class="date">'+moment(item.created_at,'YYYYMMDD').fromNow()+'</span>')
                                            ._('<span class="comments"><i class="iconfont">&#xe957;</i><b>'+item.comments.length+'</b></span>')
                                            ._('<span class="skim"><i class="iconfont">&#xe8db;</i><b>'+item.skim_num+'</b></span>')
                                        ._('</div>')
                                    ._('</div>')
                                    ._(jh)
                                ._('</a>')
                            ._('</div')
                        t._('</div>');

                        self.swiperList.append(t.toString());
                    });
                    $.hideLoading();
                    // 头部轮播
                    self.swiper();
                } 
            });
        }
    }
    user.init();    
});